
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
#include <gio/gnetworking.h>
#include "sql.h"
#include "email.h"
#include "account.h"
#include "protocol.h"
#include <libsoup/soup.h>
#define DB_NAME_ACCOUNT "account.db"
#define SERVER_PORT 12222
typedef struct {
    SoupServerMessage *msg;
    gchar *username;
} LongPollData;
static GHashTable *users = NULL;
static GQueue *messages = NULL;
static GMutex mutex;
static int
compare_strings (gconstpointer a, gconstpointer b)
{
  const char **sa = (const char **)a;
  const char **sb = (const char **)b;
  return strcmp (*sa, *sb);
}

static GString *
get_directory_listing (const char *path)
{
  GPtrArray *entries;
  GString *listing;
  char *escaped;
  GDir *dir;
  const gchar *d_name;
  int i;

  entries = g_ptr_array_new ();
  dir = g_dir_open (path, 0, NULL);
  if (dir) {
    while ((d_name = g_dir_read_name (dir))) {
      if (!strcmp (d_name, ".") ||
          (!strcmp (d_name, "..") &&
           !strcmp (path, "./")))
              continue;
      escaped = g_markup_escape_text (d_name, -1);
      g_ptr_array_add (entries, escaped);
    }
    g_dir_close (dir);
  }
  g_ptr_array_sort (entries, (GCompareFunc)compare_strings);

  listing = g_string_new ("<html>\r\n");
  escaped = g_markup_escape_text (strchr (path, '/'), -1);
  g_string_append_printf (listing, "<head><title>Index of %s</title></head>\r\n", escaped);
  g_string_append_printf (listing, "<body><h1>Index of %s</h1>\r\n<p>\r\n", escaped);
  g_free (escaped);
  for (i = 0; i < entries->len; i++) {
    g_string_append_printf (listing, "<a href=\"%s\">%s</a><br>\r\n",
                            (char *)entries->pdata[i],
                            (char *)entries->pdata[i]);
    g_free (entries->pdata[i]);
  }
  g_string_append (listing, "</body>\r\n</html>\r\n");

  g_ptr_array_free (entries, TRUE);
  return listing;
}

static void
do_get (SoupServer *server, SoupServerMessage *msg, const char *path)
{
  char *slash;
  GStatBuf st;

  if (g_stat (path, &st) == -1) {
    if (errno == EPERM)
      soup_server_message_set_status (msg, SOUP_STATUS_FORBIDDEN, NULL);
    else if (errno == ENOENT)
      soup_server_message_set_status (msg, SOUP_STATUS_NOT_FOUND, NULL);
    else
      soup_server_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR, NULL);
    return;
  }

  if (g_file_test (path, G_FILE_TEST_IS_DIR)) {
    GString *listing;
    char *index_path;

    slash = strrchr (path, '/');
    if (!slash || slash[1]) {
      char *redir_uri;
      redir_uri = g_strdup_printf ("%s/", g_uri_get_path (soup_server_message_get_uri (msg)));
      soup_server_message_set_redirect (msg, SOUP_STATUS_MOVED_PERMANENTLY,
                                 redir_uri);
      g_free (redir_uri);
      return;
    }

    index_path = g_strdup_printf ("%s/index.html", path);
    if (g_stat (index_path, &st) != -1) {
      do_get (server, msg, index_path);
      g_free (index_path);
      return;
    }
    g_free (index_path);

    listing = get_directory_listing (path);
    soup_server_message_set_response (msg, "text/html",
                               SOUP_MEMORY_TAKE,
                               listing->str, listing->len);
    soup_server_message_set_status (msg, SOUP_STATUS_OK, NULL);
    g_string_free (listing, FALSE);
    return;
  }

  if (soup_server_message_get_method (msg) == SOUP_METHOD_GET) {
    GMappedFile *mapping;
    GBytes *buffer;

    mapping = g_mapped_file_new (path, FALSE, NULL);
    if (!mapping) {
      soup_server_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR, NULL);
      return;
    }

    buffer = g_bytes_new_with_free_func (g_mapped_file_get_contents (mapping),
                                         g_mapped_file_get_length (mapping),
                                         (GDestroyNotify)g_mapped_file_unref, mapping);
    soup_message_body_append_bytes (soup_server_message_get_response_body (msg), buffer);
    g_bytes_unref (buffer);
  } else /* soup_server_message_get_method (msg) == SOUP_METHOD_HEAD */ {
    char *length;
    length = g_strdup_printf ("%lu", (gulong)st.st_size);
    soup_message_headers_append (soup_server_message_get_response_headers (msg),
                                 "Content-Length", length);
    g_free (length);
  }

  soup_server_message_set_status (msg, SOUP_STATUS_OK, NULL);
}
static void
do_put (SoupServer *server, SoupServerMessage *msg, const char *path)
{
  GStatBuf st;
  FILE *f;
  gboolean created = TRUE;
  SoupMessageBody *request_body;

  if (g_stat (path, &st) != -1) {
    const char *match = soup_message_headers_get_one (soup_server_message_get_request_headers (msg), "If-None-Match");
    if (match && !strcmp (match, "*")) {
      soup_server_message_set_status (msg, SOUP_STATUS_CONFLICT, NULL);
      return;
    }

    if (!g_file_test (path, G_FILE_TEST_IS_REGULAR)) {
      soup_server_message_set_status (msg, SOUP_STATUS_FORBIDDEN, NULL);
      return;
    }
    created = FALSE;
  }

  f = fopen (path, "w");
  if (!f) {
    soup_server_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR, NULL);
    return;
  }

  request_body = soup_server_message_get_request_body (msg);
  fwrite (request_body->data, 1, request_body->length, f);
  fclose (f);

  soup_server_message_set_status (msg, created ? SOUP_STATUS_CREATED : SOUP_STATUS_OK, NULL);
}
int http_send(void *msg, int status, const gchar *body)
{
  g_debug("L%d f-%s ", __LINE__, __func__);
  soup_server_message_set_status(msg, status, NULL);
  soup_server_message_set_response(msg, "text/plain",
      SOUP_MEMORY_TAKE, body, strlen(body));
  g_debug("L%d f-%s ----:%s", __LINE__, __func__, body);
  soup_server_message_unpause(msg);
//  soup_message_body_complete(soup_server_message_get_response_body(msg));
  return true;
}
void *users_add_msg(void *users, void *msg, const gchar *username)
{
  g_debug("L%d f-%s ", __LINE__, __func__);
  LongPollData *new_poll_data = g_new(LongPollData, 1);
  new_poll_data->msg = msg;
  new_poll_data->username = g_strdup(username);
  g_hash_table_insert(users, g_strdup(username), new_poll_data);
  return NULL;
}
void *users_get_msg(void *users, const gchar *name)
{
  g_debug("L%d f-%s ", __LINE__, __func__);
  LongPollData *poll_data = NULL;
  void *msg = NULL;

  poll_data = g_hash_table_lookup(users, name);
  if (poll_data) {
    msg = poll_data->msg;
  }
  return msg;
}
void users_remove_username(void *users, const gchar *username)
{
  g_debug("L%d f-%s ", __LINE__, __func__);
  LongPollData *poll_data = NULL;

  poll_data = g_hash_table_lookup(users, username);
  g_hash_table_remove(users, username);
  g_free(poll_data->username);
  g_free(poll_data);
}
static void timeout_finish_message(gpointer msg)
{
  g_debug("L%d f-%s ", __LINE__, __func__);
  SoupMessageHeaders *hdrs = soup_server_message_get_request_headers(msg);
  const gchar *username = soup_message_headers_get_one(hdrs, "username");
  gchar *body = "NO NEWS";

  if (users_get_msg(users, username) == msg) {
    g_debug("L%d f-%s username:%s", __LINE__, __func__, username);
    http_send(msg, SOUP_STATUS_OK, body);
    users_remove_username(users, username);
  }
}
char *messages_to_string(void *messages)
{
  g_debug("L%d f-%s ", __LINE__, __func__);
  char *res = NULL;
  GList *messages_to_send = NULL;
/*
  res = strdup("test");
  return res;
  */
  while (!g_queue_is_empty(messages)) {
    g_debug("L%d f-%s", __LINE__, __func__);
    gchar *message = (gchar *) g_queue_pop_head(messages);
    messages_to_send = g_list_prepend(messages_to_send, message);
  }
  if (messages_to_send) {
    GString *response_data = g_string_new("");
    for (GList *iter = messages_to_send; iter; iter = iter->next) {
      gchar *message = (gchar *) iter->data;
      g_string_append(response_data, message);
      g_string_append(response_data, "\n");
    }
    res = g_strdup(response_data->str);
    g_list_free_full(messages_to_send, g_free);
    g_string_free(response_data, TRUE);
  }
  return res;
}
void handle_long_polling(void *msg)
{
  g_debug("L%d f-%s ", __LINE__, __func__);
  gchar *res = NULL;
//  SoupMessageBody *request_body = soup_server_message_get_request_body(msg);
  SoupMessageHeaders *hdrs = soup_server_message_get_request_headers(msg);
  const gchar *username = soup_message_headers_get_one(hdrs, "username");
  if (!username) {
    soup_server_message_set_status(msg, SOUP_STATUS_BAD_REQUEST, NULL);
    soup_server_message_set_response(msg, "text/plain",
        SOUP_MEMORY_TAKE,
        "Missing username parameter", -1);
    return;
  }
  g_mutex_lock(&mutex);
  void *old_msg = NULL;
  old_msg = users_get_msg(users, username);
  if (old_msg) {
    users_remove_username(users, username);
    http_send(old_msg, SOUP_STATUS_INTERNAL_SERVER_ERROR, "server shutdown");
  }

  g_mutex_unlock(&mutex);
  res = messages_to_string(messages);
  if (res) {
    http_send(msg, SOUP_STATUS_OK, res);
    goto out;
  }

  users_add_msg(users, msg, username);
  soup_server_message_pause(msg);
  //g_timeout_add_once(20000, timeout_finish_message, g_object_ref(msg));
  g_timeout_add_once(20000, timeout_finish_message, g_object_ref(msg));
out:
  g_debug("L%d f-%s", __LINE__, __func__);
  return;
}
static void
server_callback (SoupServer        *server,
                 SoupServerMessage *msg,
                 const char        *path,
                 GHashTable        *query,
                 gpointer           user_data)
{
  g_debug("L%d f-%s ", __LINE__, __func__);
  char *file_path;
  SoupMessageHeadersIter iter;
  SoupMessageBody *request_body;
  const char *name, *value;

  g_print ("%s %s HTTP/1.%d\n", soup_server_message_get_method (msg), path,
           soup_server_message_get_http_version (msg));
  soup_message_headers_iter_init (&iter, soup_server_message_get_request_headers (msg));
  while (soup_message_headers_iter_next (&iter, &name, &value))
          g_print ("%s: %s\n", name, value);

  if (!g_strcmp0(path, "/ts/account/long_polling")) {
    handle_long_polling(msg);
    goto out;
  }
  request_body = soup_server_message_get_request_body (msg);
  if (request_body->length)
    g_print ("%s\n", request_body->data);

  file_path = g_strdup_printf (".%s", path);

  if (soup_server_message_get_method (msg) == SOUP_METHOD_GET || soup_server_message_get_method (msg) == SOUP_METHOD_HEAD)
    do_get (server, msg, file_path);
  else if (soup_server_message_get_method (msg) == SOUP_METHOD_PUT)
    do_put (server, msg, file_path);
  else
    soup_server_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED, NULL);

  g_free (file_path);
  g_print ("  -> %d %s\n\n", soup_server_message_get_status (msg),
      soup_server_message_get_reason_phrase (msg));
out:
  return;
}
static void
quit (int sig)
{
  g_debug("L%d f-%s ", __LINE__, __func__);
  exit (0);
}

static int port = 12222;
static const char *tls_cert_file, *tls_key_file;

static GOptionEntry entries[] = {
  { "cert-file", 'c', 0,
    G_OPTION_ARG_STRING, &tls_cert_file,
    "Use FILE as the TLS certificate file", "FILE" },
  { "key-file", 'k', 0,
    G_OPTION_ARG_STRING, &tls_key_file,
    "Use FILE as the TLS private key file", "FILE" },
  { "port", 'p', 0,
    G_OPTION_ARG_INT, &port,
    "Port to listen on", NULL },
  { NULL }
};
gboolean my_callback(gpointer user_data)
{
  g_debug("L%d f-%s ", __LINE__, __func__);
  g_mutex_lock(&mutex);
  g_queue_push_head(messages, g_strdup("new"));
  void *msg = NULL;
  const char *username = "fsdf";
  char *res = NULL;

  msg = users_get_msg(users, username);
  users_remove_username(users, username);
  res = messages_to_string(messages);
  if (msg && res) {
    http_send(msg, SOUP_STATUS_OK, res);
  }
  g_mutex_unlock(&mutex);

  return G_SOURCE_REMOVE;
}
int
main (int argc, char *argv[])
{
	g_setenv ("GSETTINGS_SCHEMA_DIR", ".", FALSE);
	g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);

  int rc = 0;
  gchar buf[1024] = {0};
  if (argc > 1) {
    g_snprintf(buf, sizeof buf, "xdg-open ./html/%s.html", argv[1]);
    system (buf);
  }
//server
  GOptionContext *opts;
  GMainLoop *loop;
  SoupServer *server;
  GSList *uris, *u;
  char *str;
  GTlsCertificate *cert;
  GError *error = NULL;

  opts = g_option_context_new (NULL);
  g_option_context_add_main_entries (opts, entries, NULL);
  if (!g_option_context_parse (opts, &argc, &argv, &error)) {
    g_printerr ("Could not parse arguments: %s\n", error->message);
    g_printerr ("%s", g_option_context_get_help (opts, TRUE, NULL));
    exit (1);
  }
  if (argc != 1) {
    g_printerr ("%s", g_option_context_get_help (opts, TRUE, NULL));
    exit (1);
  }
  g_option_context_free (opts);
  signal (SIGINT, quit);
  if (tls_cert_file && tls_key_file) {
    cert = g_tls_certificate_new_from_files (tls_cert_file, tls_key_file, &error);
    if (error) {
      g_printerr ("Unable to create server: %s\n", error->message);
      exit (1);
    }
    server = soup_server_new ("server-header", "simple-httpd ",
                              "tls-certificate", cert,
                              NULL);
    g_object_unref (cert);
    soup_server_listen_all (server, port, SOUP_SERVER_LISTEN_HTTPS, &error);
  } else {
    server = soup_server_new ("server-header", "simple-httpd ", NULL);
    soup_server_listen_all (server, port, 0, &error);
  }
  soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
//hash
  users = g_hash_table_new(g_str_hash, g_str_equal);
  messages = g_queue_new();
  g_mutex_init(&mutex);

  uris = soup_server_get_uris (server);
  for (u = uris; u; u = u->next) {
    str = g_uri_to_string (u->data);
    g_print ("Listening on %s\n", str);
    g_free (str);
    g_uri_unref (u->data);
  }
  g_slist_free (uris);
  g_print ("\nWaiting for requests...\n");

  g_timeout_add(20000, my_callback, NULL);
  loop = g_main_loop_new (NULL, TRUE);
  g_main_loop_run (loop);
  
  g_object_unref(server);
  g_hash_table_destroy(users);
  g_queue_free(messages);
  g_mutex_clear(&mutex);
  return rc;
}
